package com.honey.mybatis.generator.model.method;

import java.util.ArrayList;
import java.util.List;

import com.honey.compilation.java.Block;
import com.honey.compilation.java.Clazz;
import com.honey.compilation.java.ElseIfBlock;
import com.honey.compilation.java.IfBlock;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.Parameter;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.dbmapping.structure.Column;
import com.honey.core.types.ExceptionFullyQualifiedJavaType;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.types.JDKFullyQualifiedJavaType;
import com.honey.core.utils.JavaBeansUtil;
import com.honey.mybatis.generator.AbstractMethod;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.model.ModelConstant;

public class GetResult2Method extends AbstractMethod {
	
	private static final  String VARIABLE_RS="rs";
	
	private static final  String VARIABLE_COLUMN_NAME="columnName";
	
	private static final Parameter []PARAMETER =new Parameter[]{ 
		
		new Parameter(ModelConstant.RESULT_SET,VARIABLE_RS),
		
		new Parameter(JDKFullyQualifiedJavaType.getStringInstance(),VARIABLE_COLUMN_NAME),
	};
	
	private static final FullyQualifiedJavaType  RETURN_TYPE = JDKFullyQualifiedJavaType.getObjectInstance(); 
			
	
	private static final String[] JAVA_DOC=new String[]{
		"myBatis用户定义的类型转换，从ResultSet中获取用户定义的对象(把数据库中值转换成枚举对象)。"
	};
	
	private static final String[] ANNOTATION = new String[]{
		"Override",
	};
	
	private static final  FullyQualifiedJavaType[] EXCEPTION = new FullyQualifiedJavaType[]{
		ExceptionFullyQualifiedJavaType.getSQLExceptionInstance(),
	}; 
	
	
	public GetResult2Method(Clazz clazz, MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#addBody(com.honey.compilation.java.Method)
	 */
	@Override
	protected void addBody(Method method) {
		method.addBodyLine("Object answer = null");
		//VisibleBlock visibleBlock = new VisibleBlock ();
		
		//TryCatchBlock tryCatchBlock = new TryCatchBlock( ExceptionFullyQualifiedJavaType.getExceptionInstance() );
		//visibleBlock.addBlocks(tryCatchBlock);
		List<Column> list = new ArrayList<Column>();
		for(Column column : introspectSchemaColumn.allColumns()){
			if( column.getEnumSchema() != null ){
				list.add(column);
			}
		}
		if( list.size() == 1){
			Column  column = list.get(0);
			String entityName =super.getModelClassName(introspectSchema.getSchema());
			String enumName =super.getStateEnumName(column.getEnumSchema());
			String typeName = column.getFullyQualifiedJavaType().getBaseShortName();
			String methodName =JavaBeansUtil.getGetterMethodName(typeName) ;
			method.addBodyLine(typeName+" value = " + VARIABLE_RS + "."+methodName+"( columnName )");
			method.addBodyLine("answer = " + entityName+"."+enumName+".getInstance( value )");
		}else{
			boolean bool = true ;
			Block block =null;
			String entityName =super.getModelPrimitiveClassName(introspectSchema.getSchema());
			for(Column column : list){
				if( column.getEnumSchema() != null ){
					String enumName =super.getStateEnumName(column.getEnumSchema());
					if(bool){block = new IfBlock(super.getTableColumnEnumClassName(introspectSchema.getSchema()) +"."+mybatisPlugin.getJavaNameCalculator().fieldNameCalculator(column.getJavaProperty()).toUpperCase()+".toString().equals( columnName ) ");bool = false;
					}else{block = new ElseIfBlock(super.getTableColumnEnumClassName(introspectSchema.getSchema()) +"."+mybatisPlugin.getJavaNameCalculator().fieldNameCalculator(column.getJavaProperty()).toUpperCase()+".toString().equals( columnName ) ");}
					
					String typeName = column.getFullyQualifiedJavaType().getBaseShortName();
					String methodName =JavaBeansUtil.getGetterMethodName(typeName) ;
					block.addBodyLine(typeName+" value = " + VARIABLE_RS + "."+methodName+"( columnName )");
					block.addBodyLine("answer = "+entityName+"."+enumName+".getInstance( value )");
					method.addBlocks(block);
				}
			}
		}
		
		//method.addBlocks(visibleBlock);
		method.addBodyLine("return answer");
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodName()
	 */
	@Override
	protected String getMethodName() {

		return ModelConstant.GET_RESULT_METHOD;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getParameter()
	 */
	@Override
	protected Parameter[] getParameter() {
		return PARAMETER;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getJavaDoc()
	 */
	@Override
	protected String[] getJavaDoc() {
		return JAVA_DOC;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getAnnotation()
	 */
	@Override
	protected String[] getAnnotation() {
		return ANNOTATION;
	}

	/* (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getReturnType()
	 */
	@Override
	protected FullyQualifiedJavaType getReturnType() {
		return RETURN_TYPE;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getException()
	 */
	@Override
	protected FullyQualifiedJavaType[] getException() {
		return EXCEPTION;
	}
	
	
}
